package com.dingding.queue.base.push;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.facade.queue.dto.push.PushRequestDTO;
import com.dingding.facade.queue.enums.ClientType;
import com.dingding.facade.queue.enums.RoleType;

/**
 * 
 * @ClassName: PushDispatcher 
 * @Description: 推送调度类
 * @author CHEHAUNBO 
 * @date 2016年6月7日 下午4:16:33
 * 
 */
@Component("pushDispatcher")
public class PushDispatcher implements IPushDispatcher {

	private static Logger log = Logger.getLogger(PushDispatcher.class);

	@Resource(name = "andoridSendPush")
	private AbstractSendPush androidSendPush;
	@Resource(name = "iosSendPush")
	private AbstractSendPush iosSendPush;

	@Override
	public boolean dispatcher(PushRequestDTO pushRequestDTO) {

		if (pushRequestDTO == null || pushRequestDTO.getClientType() == null || pushRequestDTO.getPushType() == null) {
			log.error("消息队列中的内容出现存在空值，不作消息处理！" + pushRequestDTO.toString());
			return true;
		}

		AbstractSendPush abstractSendPush = null;

		if (pushRequestDTO.getClientType() == ClientType.Android) {
			abstractSendPush = androidSendPush;
		} else {
			abstractSendPush = iosSendPush;
		}

		RoleType roleType = RoleType.Passenger;

		if (pushRequestDTO.getRoleType() != null) {
			roleType = pushRequestDTO.getRoleType();
		}

		boolean flag = false;
		switch (pushRequestDTO.getPushType()) {
		case alert_msgcontent:
			flag = abstractSendPush.sendPush(pushRequestDTO.getAliasId(), pushRequestDTO.getAlert(),
					pushRequestDTO.getPushContent(), roleType);
			break;
		case alert_msgtitle_msgcontent_map:
			flag = abstractSendPush.sendPush(pushRequestDTO.getAliasId(), pushRequestDTO.getAlert(),
					pushRequestDTO.getMsgTitle(), pushRequestDTO.getPushContent(), pushRequestDTO.getMap(), roleType);
			break;
		case msgcontent:
			flag = abstractSendPush.sendPush(pushRequestDTO.getAliasId(), pushRequestDTO.getPushContent(), roleType);
			break;
		case alert_extra:
			flag = abstractSendPush.sendPush(pushRequestDTO.getAliasId(), pushRequestDTO.getAlert(),
					pushRequestDTO.getMap(), roleType);
			break;
		}

		return flag;
	}

}
